<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>javascript数据结构与算法 | 茂茂个人博客</title>
    <meta name="generator" content="VuePress 1.7.1">
    <link rel="icon" href="https://pan.zealsay.com/blog/favicon.ico">
    <script>
          var _hmt = _hmt || [];
          (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?b0aae218897fa9d8a9f76e9a77e0b3c6";
            var s = document.getElementsByTagName("script")[0]; 
            s.parentNode.insertBefore(hm, s);
          })();
        </script>
    <meta name="description" content="开心最重要呀">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/blog/assets/css/0.styles.ed123d92.css" as="style"><link rel="preload" href="/blog/assets/js/app.e4cba77b.js" as="script"><link rel="preload" href="/blog/assets/js/5.1ad0ff24.js" as="script"><link rel="preload" href="/blog/assets/js/1.fe4eac58.js" as="script"><link rel="preload" href="/blog/assets/js/52.c11bc4a4.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.58980e71.js"><link rel="prefetch" href="/blog/assets/js/11.461ee1ab.js"><link rel="prefetch" href="/blog/assets/js/12.de9aade4.js"><link rel="prefetch" href="/blog/assets/js/13.c1f7abfc.js"><link rel="prefetch" href="/blog/assets/js/14.eeee4da5.js"><link rel="prefetch" href="/blog/assets/js/15.225a74ee.js"><link rel="prefetch" href="/blog/assets/js/16.3eef12b1.js"><link rel="prefetch" href="/blog/assets/js/17.a97bd7c7.js"><link rel="prefetch" href="/blog/assets/js/18.461a74bd.js"><link rel="prefetch" href="/blog/assets/js/19.7b390d19.js"><link rel="prefetch" href="/blog/assets/js/20.8638de7c.js"><link rel="prefetch" href="/blog/assets/js/21.8b1e5c62.js"><link rel="prefetch" href="/blog/assets/js/22.b6b97b5b.js"><link rel="prefetch" href="/blog/assets/js/23.c3e9364b.js"><link rel="prefetch" href="/blog/assets/js/24.daa0ce9e.js"><link rel="prefetch" href="/blog/assets/js/25.0c44cdb2.js"><link rel="prefetch" href="/blog/assets/js/26.ed604668.js"><link rel="prefetch" href="/blog/assets/js/27.0c4acdea.js"><link rel="prefetch" href="/blog/assets/js/28.4f896280.js"><link rel="prefetch" href="/blog/assets/js/29.50e69b67.js"><link rel="prefetch" href="/blog/assets/js/3.90606e56.js"><link rel="prefetch" href="/blog/assets/js/30.5c69a6a2.js"><link rel="prefetch" href="/blog/assets/js/31.8a40c25d.js"><link rel="prefetch" href="/blog/assets/js/32.7a1f46ed.js"><link rel="prefetch" href="/blog/assets/js/33.a48429b4.js"><link rel="prefetch" href="/blog/assets/js/34.8f224bd0.js"><link rel="prefetch" href="/blog/assets/js/35.bc552eb5.js"><link rel="prefetch" href="/blog/assets/js/36.7e0cc0d9.js"><link rel="prefetch" href="/blog/assets/js/37.2bb21bfb.js"><link rel="prefetch" href="/blog/assets/js/38.f77b014f.js"><link rel="prefetch" href="/blog/assets/js/39.8a5ba8b0.js"><link rel="prefetch" href="/blog/assets/js/4.89c80be7.js"><link rel="prefetch" href="/blog/assets/js/40.c600c684.js"><link rel="prefetch" href="/blog/assets/js/41.0dbac397.js"><link rel="prefetch" href="/blog/assets/js/42.f1035a7b.js"><link rel="prefetch" href="/blog/assets/js/43.ace95e0e.js"><link rel="prefetch" href="/blog/assets/js/44.0ff283b9.js"><link rel="prefetch" href="/blog/assets/js/45.836151e7.js"><link rel="prefetch" href="/blog/assets/js/46.033824d7.js"><link rel="prefetch" href="/blog/assets/js/47.499ce120.js"><link rel="prefetch" href="/blog/assets/js/48.39c2db90.js"><link rel="prefetch" href="/blog/assets/js/49.ca266dff.js"><link rel="prefetch" href="/blog/assets/js/50.c12ee57e.js"><link rel="prefetch" href="/blog/assets/js/51.4c3c8a1c.js"><link rel="prefetch" href="/blog/assets/js/53.d4dc4ec7.js"><link rel="prefetch" href="/blog/assets/js/54.300dd272.js"><link rel="prefetch" href="/blog/assets/js/55.6e86af02.js"><link rel="prefetch" href="/blog/assets/js/56.d91a04d8.js"><link rel="prefetch" href="/blog/assets/js/6.4f77b856.js"><link rel="prefetch" href="/blog/assets/js/7.db9a864b.js"><link rel="prefetch" href="/blog/assets/js/8.bf0cca83.js"><link rel="prefetch" href="/blog/assets/js/9.84ddd18e.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.ed123d92.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-0c5314fc><div data-v-0c5314fc><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-0c5314fc data-v-0c5314fc><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-5cb9a64e data-v-0c5314fc data-v-0c5314fc><h3 class="title" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e>茂茂个人博客</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e><input type="password" value="" data-v-5cb9a64e> <span data-v-5cb9a64e>Konck! Knock!</span> <button data-v-5cb9a64e>OK</button></label> <div class="footer" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e><span data-v-5cb9a64e><i class="iconfont reco-theme" data-v-5cb9a64e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-5cb9a64e>vuePress-theme-reco</a></span> <span data-v-5cb9a64e><i class="iconfont reco-copyright" data-v-5cb9a64e></i> <a data-v-5cb9a64e><span data-v-5cb9a64e>茂茂</span>
            
          <span data-v-5cb9a64e>2019 - </span>
          2023
        </a></span></div></div> <div class="hide" data-v-0c5314fc><div data-v-0c5314fc><div id="smart" class="wrapper-page" style="background-image:url(/blog/articleImage/img09.jpeg);background-position-x:center;background-position-y:center;background-size:cover;background-repeat-x:no-repeat;background-repeat-y:no-repeat;" data-v-0c5314fc><header class="navbar" data-v-0c5314fc><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><img src="/blog/logo.png" alt="茂茂个人博客" class="logo"> <span class="site-name">茂茂个人博客</span></a> <div class="links"><div id="dayNightSwitch" class="generalWrapper" data-v-32f44868><a class="click" data-v-32f44868><div class="onOff daySwitch" data-v-32f44868><div class="star star1" data-v-32f44868></div> <div class="star star2" data-v-32f44868></div> <div class="star star3" data-v-32f44868></div> <div class="star star4" data-v-32f44868></div> <div class="star star5" data-v-32f44868></div> <div class="star sky" data-v-32f44868></div> <div class="sunMoon" data-v-32f44868><div class="crater crater1" data-v-32f44868></div> <div class="crater crater2" data-v-32f44868></div> <div class="crater crater3" data-v-32f44868></div> <div class="cloud part1" data-v-32f44868></div> <div class="cloud part2" data-v-32f44868></div></div></div></a></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      博客
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/工具与阅读/" class="nav-link"><i class="iconfont undefined"></i>
  工具与阅读
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/vue/" class="nav-link"><i class="iconfont undefined"></i>
  vue
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/后端/" class="nav-link"><i class="iconfont undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/node/" class="nav-link"><i class="iconfont undefined"></i>
  node
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/IOS/" class="nav-link"><i class="iconfont undefined"></i>
  IOS
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/WebGIS/" class="nav-link"><i class="iconfont undefined"></i>
  WebGIS
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/React/" class="nav-link"><i class="iconfont undefined"></i>
  React
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/面试/" class="nav-link"><i class="iconfont undefined"></i>
  面试
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/Angular/" class="nav-link"><i class="iconfont undefined"></i>
  Angular
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/数据结构与算法/" class="nav-link"><i class="iconfont undefined"></i>
  数据结构与算法
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/微前端架构/" class="nav-link"><i class="iconfont undefined"></i>
  微前端架构
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><a href="/blog/about/" class="nav-link"><i class="iconfont reco-account"></i>
  关于
</a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-0c5314fc></div> <aside class="sidebar" data-v-0c5314fc><div class="personal-info-wrapper" data-v-03833281 data-v-0c5314fc><img src="/blog/logo.png" alt="author-avatar" class="personal-img" data-v-03833281> <h3 class="name" data-v-03833281>
    茂茂
  </h3> <div class="num" data-v-03833281><div data-v-03833281><h3 data-v-03833281>43</h3> <h6 data-v-03833281>文章</h6></div> <div data-v-03833281><h3 data-v-03833281>30</h3> <h6 data-v-03833281>标签</h6></div></div> <hr data-v-03833281></div> <nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link"><i class="iconfont reco-home"></i>
  主页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      博客
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/categories/工具与阅读/" class="nav-link"><i class="iconfont undefined"></i>
  工具与阅读
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/vue/" class="nav-link"><i class="iconfont undefined"></i>
  vue
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/前端/" class="nav-link"><i class="iconfont undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/后端/" class="nav-link"><i class="iconfont undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/node/" class="nav-link"><i class="iconfont undefined"></i>
  node
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/IOS/" class="nav-link"><i class="iconfont undefined"></i>
  IOS
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/WebGIS/" class="nav-link"><i class="iconfont undefined"></i>
  WebGIS
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/React/" class="nav-link"><i class="iconfont undefined"></i>
  React
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/面试/" class="nav-link"><i class="iconfont undefined"></i>
  面试
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/Angular/" class="nav-link"><i class="iconfont undefined"></i>
  Angular
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/数据结构与算法/" class="nav-link"><i class="iconfont undefined"></i>
  数据结构与算法
</a></li><li class="dropdown-item"><!----> <a href="/blog/categories/微前端架构/" class="nav-link"><i class="iconfont undefined"></i>
  微前端架构
</a></li></ul></div></div><div class="nav-item"><a href="/blog/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/blog/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div><div class="nav-item"><a href="/blog/about/" class="nav-link"><i class="iconfont reco-account"></i>
  关于
</a></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-5cb9a64e data-v-0c5314fc><h3 class="title" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e>javascript数据结构与算法</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e><input type="password" value="" data-v-5cb9a64e> <span data-v-5cb9a64e>Konck! Knock!</span> <button data-v-5cb9a64e>OK</button></label> <div class="footer" style="display:none;" data-v-5cb9a64e data-v-5cb9a64e><span data-v-5cb9a64e><i class="iconfont reco-theme" data-v-5cb9a64e></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-5cb9a64e>vuePress-theme-reco</a></span> <span data-v-5cb9a64e><i class="iconfont reco-copyright" data-v-5cb9a64e></i> <a data-v-5cb9a64e><span data-v-5cb9a64e>茂茂</span>
            
          <span data-v-5cb9a64e>2019 - </span>
          2023
        </a></span></div></div></div> <div data-v-0c5314fc><main class="page" style="padding-right:0;"><div class="page-title" style="display:none;"><h1 class="title">javascript数据结构与算法</h1> <div class="page-info" data-v-0efa1f05><i class="iconfont reco-account" data-v-0efa1f05><span data-v-0efa1f05>茂茂</span></i> <i class="iconfont reco-date" data-v-0efa1f05><span data-v-0efa1f05>2023-02-14</span></i> <i class="iconfont reco-eye" data-v-0efa1f05><span id="/blog/blogs/tec/2023/021401.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-0efa1f05><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-0efa1f05><span class="tag-item" data-v-0efa1f05>数据结构与算法</span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h2 id="一-初识数据结构与算法"><a href="#一-初识数据结构与算法" class="header-anchor">#</a> 一. 初识数据结构与算法</h2> <blockquote><ul><li>“数据结构是数据对象，以及存在于该对象的实例和组成实例的数据元素之间的各种联系。这些联系可以通过定义相关的函数来给出。”——Sartaj Sahni，《数据结构、算法与应用》</li> <li>“数据结构是ADT（抽象数据类型 Abstract DataType）的物理实现。”—— Clifford A.Shaffer，《数据结构与算法分析》</li> <li>“数据结构（data structure）是计算机中存储、组织数据的方式。通常情况下，精心选择的数据结构可以带来最优效率的算法。”
——中文维基百科</li></ul></blockquote> <h4 id="_1-常见的数据结构-存储数据的形式"><a href="#_1-常见的数据结构-存储数据的形式" class="header-anchor">#</a> 1. 常见的数据结构 --- 存储数据的形式</h4> <p>数据不同的存储形式，影响着数据获取的效率，所以我们应该使用最有效的数据结构去存数据。</p> <ul><li>数组（Aarray）</li> <li>栈（Stack）</li> <li>链表（Linked List）</li> <li>图（Graph）</li> <li>散列表（Hash）</li> <li>队列（Queue）</li> <li>堆（Heap）</li> <li>树（Tree）</li></ul> <h4 id="_2-算法"><a href="#_2-算法" class="header-anchor">#</a> 2. 算法</h4> <p>算法（Algorithm）是指解题方案的准确而完整的描述，是一系列解决问题的清晰指令，算法代表着用系统的方法描述解决问题的策略机制。也就是说，能够对一定规范的输入，在有限时间内获得所要求的输出。如果一个算法有缺陷，或不适合于某个问题，执行这个算法将不会解决这个问题。不同的算法可能用不同的时间，空间或效率来完成同样的任务。一个算法的优劣可以用空间复杂度与时间复杂度来衡量。</p> <img src="/blog/堆排序.gif" alt="堆排序"> <img src="/blog/快速.gif" alt="快速"> <h2 id="二-数组结构"><a href="#二-数组结构" class="header-anchor">#</a> 二. 数组结构</h2> <ul><li><p>什么是数组？</p></li> <li><p>字面理解就是 <strong>数字的组合</strong></p></li> <li><p>其实不太准确，准确的来说数组是一个 <strong>数据的集合</strong></p></li> <li><p>也就是我们把一些数据放在一个盒子里面，按照顺序排好</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token string">'hello'</span><span class="token punctuation">,</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">]</span>
</code></pre></div></li> <li><p>这个东西就是一个数组，存储着一些数据的集合</p></li></ul> <p><strong>数据类型分类</strong></p> <ul><li><p><code>number</code> / <code>string</code> / <code>boolean</code> / <code>undefined</code> / <code>null</code> / <code>object</code> / <code>function</code>  /  <code>array</code> / ...</p></li> <li><p>数组也是数据类型中的一种</p></li> <li><p>我们简单的把所有数据类型分为两个大类 <strong>基本数据类型</strong> 和 <strong>复杂数据类型</strong></p></li> <li><p>基本数据类型： <code>number</code> / <code>string</code> / <code>boolean</code> / <code>undefined</code> / <code>null</code></p></li> <li><p>复杂数据类型： <code>object</code> / <code>function</code>  /  <code>array</code> / ...</p></li></ul> <h4 id="_1-创建一个数组"><a href="#_1-创建一个数组" class="header-anchor">#</a> 1 创建一个数组</h4> <ul><li>数组就是一个 <code>[]</code></li> <li>在 <code>[]</code> 里面存储着各种各样的数据，按照顺序依次排好</li></ul> <p><strong>字面量创建一个数组</strong></p> <ul><li><p>直接使用 <code>[]</code> 的方式创建一个数组</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// 创建一个空数组</span>
<span class="token keyword">var</span> arr1 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>

<span class="token comment">// 创建一个有内容的数组</span>
<span class="token keyword">var</span> arr2 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>
</code></pre></div></li></ul> <p><strong>内置构造函数创建数组</strong></p> <ul><li><p>使用 <code>js</code> 的内置构造函数 <code>Array</code> 创建一个数组</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// 创建一个空数组</span>
<span class="token keyword">var</span> arr1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment">// 创建一个长度为 10 的数组</span>
<span class="token keyword">var</span> arr2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>

<span class="token comment">// 创建一个有内容的数组</span>
<span class="token keyword">var</span> arr3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span>
</code></pre></div></li></ul> <h4 id="_2-数组的-length"><a href="#_2-数组的-length" class="header-anchor">#</a> 2 数组的 length</h4> <ul><li><p><code>length</code>:  长度的意思</p></li> <li><p><code>length</code> 就是表示数组的长度，数组里面有多少个成员，<code>length</code> 就是多少</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// 创建一个数组</span>
<span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span>length<span class="token punctuation">)</span> <span class="token comment">// 3</span>
</code></pre></div></li></ul> <h4 id="_3-数组的索引"><a href="#_3-数组的索引" class="header-anchor">#</a> 3 数组的索引</h4> <ul><li><p>索引，也叫做下标，是指一个数据在数组里面排在第几个的位置</p></li> <li><p>注意： <strong>在所有的语言里面，索引都是从 0 开始的</strong></p></li> <li><p>在 <code>js</code> 里面也一样，数组的索引从 0 开始</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// 创建一个数组</span>
<span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'hello'</span><span class="token punctuation">,</span> <span class="token string">'world'</span><span class="token punctuation">]</span>
</code></pre></div></li> <li><p>上面这个数组中，<strong>第 0 个</strong> 数据就是字符串 <code>hello</code>，<strong>第 1 个</strong> 数据就是字符串 <code>world</code></p></li> <li><p>想获取数组中的第几个就使用 <code>数组[索引]</code> 来获取</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'hello'</span><span class="token punctuation">,</span> <span class="token string">'world'</span><span class="token punctuation">]</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// hello</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// world</span>
</code></pre></div></li></ul> <h4 id="_4-数组的常用方法"><a href="#_4-数组的常用方法" class="header-anchor">#</a> 4 数组的常用方法</h4> <ul><li><p>数组是一个复杂数据类型，我们在操作它的时候就不能再想基本数据类型一样操作了</p></li> <li><p>比如我们想改变一个数组</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token comment">// 创建一个数组</span>
<span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>

<span class="token comment">// 我们想把数组变成只有 1 和 2</span>
arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span>
</code></pre></div><ul><li>这样肯定是不合理，因为这样不是在改变之前的数组</li> <li>相当于心弄了一个数组给到 <code>arr</code> 这个变量了</li> <li>相当于把 <code>arr</code> 里面存储的地址给换了，也就是把存储空间换掉了，而不是在之前的空间里面修改</li> <li>所以我们就需要借助一些方法，在不改变存储空间的情况下，把存储空间里面的数据改变了</li></ul></li></ul> <p><strong>数组常用方法之 push</strong></p> <ul><li><p><code>push</code> 是用来在数组的末尾追加一个元素</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 push 方法追加一个元素在末尾</span>
arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment">// [1, 2, 3, 4]</span>
</code></pre></div></li></ul> <p><strong>数组常用方法之 pop</strong></p> <ul><li><p><code>pop</code> 是用来删除数组末尾的一个元素</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 pop 方法删除末尾的一个元素</span>
arr<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment">// [1, 2]</span>
</code></pre></div></li></ul> <p><strong>数组常用方法之 unshift</strong></p> <ul><li><p><code>unshift</code> 是在数组的最前面添加一个元素</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 unshift 方法想数组的最前面添加一个元素</span>
arr<span class="token punctuation">.</span><span class="token function">unshift</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment">// [4, 1, 2, 3]</span>
</code></pre></div></li></ul> <p><strong>数组常用方法之  shift</strong></p> <ul><li><p><code>shift</code> 是删除数组最前面的一个元素</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 shift 方法删除数组最前面的一个元素</span>
arr<span class="token punctuation">.</span><span class="token function">shift</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment">// [2, 3]</span>
</code></pre></div></li></ul> <p><strong>数组常用方法之 splice</strong></p> <ul><li><p><code>splice</code> 是截取数组中的某些内容，按照数组的索引来截取</p></li> <li><p>语法： <code>splice(从哪一个索引位置开始，截取多少个，替换的新元素)</code> （第三个参数可以不写）</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 splice 方法截取数组</span>
arr<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment">// [1, 4, 5]</span>
</code></pre></div><ul><li><code>arr.splice(1, 2)</code> 表示从索引 1 开始截取 2 个内容</li> <li>第三个参数没有写，就是没有新内容替换掉截取位置</li></ul> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 splice 方法截取数组</span>
arr<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">'我是新内容'</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment">// [1, '我是新内容', 4, 5]</span>
</code></pre></div><ul><li><code>arr.splice(1, 2, '我是新内容')</code> 表示从索引 1 开始截取 2 个内容</li> <li>然后用第三个参数把截取完空出来的位置填充</li></ul></li></ul> <p><strong>数组常用方法之 reverse</strong></p> <ul><li><p><code>reverse</code> 是用来反转数组使用的</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 reverse 方法来反转数组</span>
arr<span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment">// [3, 2, 1]</span>
</code></pre></div></li></ul> <p><strong>数组常用方法之 sort</strong></p> <ul><li><p><code>sort</code> 是用来给数组排序的</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 sort 方法给数组排序</span>
arr<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment">// [1, 2, 3]</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code> <span class="token keyword">let</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">]</span>

          arr<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span>y</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span>x<span class="token operator">-</span>y<span class="token punctuation">)</span>
          arr<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span>y</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span>y<span class="token operator">-</span>x<span class="token punctuation">)</span>

          <span class="token keyword">let</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>
              name<span class="token operator">:</span><span class="token string">&quot;kerwin&quot;</span><span class="token punctuation">,</span>
              age<span class="token operator">:</span><span class="token number">100</span>
          <span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span>
              name<span class="token operator">:</span><span class="token string">&quot;teichui&quot;</span><span class="token punctuation">,</span>
              age<span class="token operator">:</span><span class="token number">18</span>
          <span class="token punctuation">}</span><span class="token punctuation">,</span><span class="token punctuation">{</span>
              name<span class="token operator">:</span><span class="token string">&quot;xiaoming&quot;</span><span class="token punctuation">,</span>
              age<span class="token operator">:</span><span class="token number">20</span>
          <span class="token punctuation">}</span><span class="token punctuation">]</span>

          arr<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">x<span class="token punctuation">,</span>y</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span>y<span class="token punctuation">.</span>age<span class="token operator">-</span>x<span class="token punctuation">.</span>age<span class="token punctuation">)</span>
</code></pre></div><ul><li>这个只是一个基本的简单用法</li></ul></li></ul> <p><strong>数组常用方法之 concat</strong></p> <ul><li><p><code>concat</code> 是把多个数组进行拼接</p></li> <li><p>和之前的方法有一些不一样的地方，就是 <code>concat</code> 不会改变原始数组，而是返回一个新的数组</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 concat 方法拼接数组</span>
<span class="token keyword">var</span> newArr <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment">// [1, 2, 3]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newArr<span class="token punctuation">)</span> <span class="token comment">// [1, 2, 3, 4, 5, 6]</span>
</code></pre></div><ul><li>注意： <strong><code>concat</code> 方法不会改变原始数组</strong></li></ul></li></ul> <p><strong>数组常用方法之 join</strong></p> <ul><li><p><code>join</code> 是把数组里面的每一项内容链接起来，变成一个字符串</p></li> <li><p>可以自己定义每一项之间链接的内容 <code>join(要以什么内容链接)</code></p></li> <li><p>不会改变原始数组，而是把链接好的字符串返回</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 join 链接数组</span>
<span class="token keyword">var</span> str <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">'-'</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span> <span class="token comment">// [1, 2, 3]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span> <span class="token comment">// 1-2-3</span>
</code></pre></div><ul><li>注意： <strong>join 方法不会改变原始数组，而是返回链接好的字符串</strong></li></ul></li></ul> <h5 id="搜索-indexof-lastindexof-find-findindex-findlast-findlastindex-includes"><a href="#搜索-indexof-lastindexof-find-findindex-findlast-findlastindex-includes" class="header-anchor">#</a> 搜索 indexOf, lastIndexOf  find findIndex findLast findLastIndex  includes</h5> <div class="language-js extra-class"><pre class="language-js"><code>

<span class="token keyword">let</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">,</span><span class="token number">13</span><span class="token punctuation">,</span><span class="token number">9</span><span class="token punctuation">]</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span><span class="token function">includes</span><span class="token punctuation">(</span><span class="token number">15</span><span class="token punctuation">)</span><span class="token punctuation">)</span>


<span class="token keyword">let</span> res <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token operator">=&gt;</span>item<span class="token operator">&gt;</span><span class="token number">10</span><span class="token punctuation">)</span>
<span class="token keyword">let</span> res <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">findIndex</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token operator">=&gt;</span>item<span class="token operator">&gt;</span><span class="token number">10</span><span class="token punctuation">)</span>
<span class="token keyword">let</span> res <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">findLast</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token operator">=&gt;</span>item<span class="token operator">&gt;</span><span class="token number">10</span><span class="token punctuation">)</span>
<span class="token keyword">let</span> res <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">findLastIndex</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token operator">=&gt;</span>item<span class="token operator">&gt;</span><span class="token number">10</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>
</code></pre></div><p><strong>数组常用方法之 indexOf</strong></p> <ul><li><p><code>indexOf</code> 用来找到数组中某一项的索引</p></li> <li><p>语法： <code>indexOf(你要找的数组中的项)</code></p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 indexOf 超找数组中的某一项</span>
<span class="token keyword">var</span> index <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span> <span class="token comment">// 2</span>
</code></pre></div><ul><li>我们要找的是数组中值为 3 的那一项</li> <li>返回的就是值为 3 的那一项在该数组中的索引</li></ul></li> <li><p>如果你要找的内容在数组中没有，那么就会返回 -1</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 indexOf 超找数组中的某一项</span>
<span class="token keyword">var</span> index <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>index<span class="token punctuation">)</span> <span class="token comment">// -1</span>
</code></pre></div><ul><li>你要找的值在数组中不存在，那么就会返回 -1</li></ul></li></ul> <h5 id="迭代方法-every-some-filter-map-foreach-reduce"><a href="#迭代方法-every-some-filter-map-foreach-reduce" class="header-anchor">#</a> 迭代方法 every ,some ,filter ,map ,foreach ,reduce</h5> <div class="language-js extra-class"><pre class="language-js"><code>       <span class="token keyword">let</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">,</span><span class="token number">13</span><span class="token punctuation">]</span>

       <span class="token keyword">let</span> res <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">every</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token operator">=&gt;</span>item<span class="token operator">&gt;</span><span class="token number">10</span><span class="token punctuation">)</span>  <span class="token comment">//false</span>
       <span class="token keyword">let</span> res <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">some</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token operator">=&gt;</span>item<span class="token operator">&gt;</span><span class="token number">12</span><span class="token punctuation">)</span>  <span class="token comment">//true</span>
       <span class="token keyword">let</span> res <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token operator">=&gt;</span>item<span class="token operator">&gt;</span><span class="token number">12</span><span class="token punctuation">)</span> <span class="token comment">//[13]</span>
       <span class="token keyword">let</span> res <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token parameter">item</span><span class="token operator">=&gt;</span>item<span class="token operator">+</span><span class="token string">&quot;kerwin&quot;</span><span class="token punctuation">)</span> <span class="token comment">//映射 [10kerwin,11kerwin,....]</span>

       <span class="token keyword">let</span> res <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item1<span class="token punctuation">,</span>item2</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span>item1<span class="token operator">+</span>item2<span class="token punctuation">)</span> <span class="token comment">//累加、累乘 item1 返回的是每次计算的结果</span>
       <span class="token comment">// 10，11  10+11</span>
       <span class="token comment">// 21，12  21+12</span>


       console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span>

       arr<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span>index</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
           console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>item<span class="token punctuation">,</span>index<span class="token punctuation">)</span>
       <span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><p><strong>数组常用方法之 forEach</strong></p> <ul><li><p>和 <code>for</code> 循环一个作用，就是用来遍历数组的</p></li> <li><p>语法：<code>arr.forEach(function (item, index, arr) {})</code></p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 forEach 遍历数组</span>
arr<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span> index<span class="token punctuation">,</span> arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// item 就是数组中的每一项</span>
  <span class="token comment">// index 就是数组的索引</span>
  <span class="token comment">// arr 就是原始数组</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'数组的第 '</span> <span class="token operator">+</span> index <span class="token operator">+</span> <span class="token string">' 项的值是 '</span> <span class="token operator">+</span> item <span class="token operator">+</span> <span class="token string">'，原始数组是'</span><span class="token punctuation">,</span> arr<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre></div><ul><li><code>forEach()</code> 的时候传递的那个函数，会根据数组的长度执行</li> <li>数组的长度是多少，这个函数就会执行多少回</li></ul></li></ul> <p><strong>数组常用方法之 map</strong></p> <ul><li><p>和 <code>forEach</code> 类似，只不过可以对数组中的每一项进行操作，返回一个新的数组</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 map 遍历数组</span>
<span class="token keyword">var</span> newArr <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span> index<span class="token punctuation">,</span> arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// item 就是数组中的每一项</span>
  <span class="token comment">// index 就是数组的索引</span>
  <span class="token comment">// arr 就是原始数组</span>
  <span class="token keyword">return</span> item <span class="token operator">+</span> <span class="token number">10</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newArr<span class="token punctuation">)</span> <span class="token comment">// [11, 12, 13]</span>
</code></pre></div></li></ul> <p><strong>数组常用方法之 filter</strong></p> <ul><li><p>和 <code>map</code> 的使用方式类似，按照我们的条件来筛选数组</p></li> <li><p>把原始数组中满足条件的筛选出来，组成一个新的数组返回</p> <div class="language-javascript extra-class"><pre class="language-javascript"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span>

<span class="token comment">// 使用 filter 过滤数组</span>
<span class="token keyword">var</span> newArr <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span> index<span class="token punctuation">,</span> arr</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// item 就是数组中的每一项</span>
  <span class="token comment">// index 就是数组的索引</span>
  <span class="token comment">// arr 就是原始数组</span>
  <span class="token keyword">return</span> item <span class="token operator">&gt;</span> <span class="token number">1</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>newArr<span class="token punctuation">)</span> <span class="token comment">// [2, 3]</span>
</code></pre></div><ul><li>我们设置的条件就是 <code>&gt; 1</code></li> <li>返回的新数组就会是原始数组中所有 <code>&gt; 1</code> 的项</li></ul></li></ul> <h5 id="迭代器对象"><a href="#迭代器对象" class="header-anchor">#</a> 迭代器对象</h5> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">let</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">11</span><span class="token punctuation">,</span><span class="token number">12</span><span class="token punctuation">,</span><span class="token number">13</span><span class="token punctuation">]</span>

 console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span>

<span class="token keyword">let</span> ite <span class="token operator">=</span> arr<span class="token punctuation">[</span>Symbol<span class="token punctuation">.</span>iterator<span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>ite<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>ite<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>ite<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>ite<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>ite<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>


<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token keyword">of</span> arr<span class="token punctuation">)</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
<span class="token punctuation">}</span>

console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span><span class="token function">entries</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token keyword">of</span> arr<span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span>
<span class="token punctuation">}</span>


<span class="token comment">// Array.from</span>
<span class="token keyword">function</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>Array<span class="token punctuation">.</span><span class="token function">from</span><span class="token punctuation">(</span>arguments<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token function">test</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span>



</code></pre></div><h5 id="二维数组"><a href="#二维数组" class="header-anchor">#</a> 二维数组</h5> <div class="language-js extra-class"><pre class="language-js"><code>
<span class="token keyword">let</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span>
    <span class="token punctuation">[</span><span class="token number">98</span><span class="token punctuation">,</span> <span class="token number">99</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">99</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
    <span class="token punctuation">[</span><span class="token number">80</span><span class="token punctuation">,</span> <span class="token number">80</span><span class="token punctuation">,</span> <span class="token number">69</span><span class="token punctuation">]</span>
<span class="token punctuation">]</span>

<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">第</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span>i<span class="token operator">+</span><span class="token number">1</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">个人的成绩</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">)</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span>length<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">2023/2/14 下午3:50:42</span></div></footer> <!----> <!----> <!----></main> <!----></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><div class="kanbanniang" data-v-5775ee02><div class="banniang-container" style="display:;" data-v-5775ee02><div class="messageBox" style="right:68px;bottom:190px;display:none;" data-v-5775ee02>
      我是王茂铭谢谢你的关注 
    </div> <div class="operation" style="right:90px;bottom:40px;display:;" data-v-5775ee02><i class="kbnfont kbn-ban-home ban-home" data-v-5775ee02></i> <i class="kbnfont kbn-ban-message message" data-v-5775ee02></i> <i class="kbnfont kbn-ban-close close" data-v-5775ee02></i> <a target="_blank" href="https://vuepress-theme-reco.recoluan.com/views/plugins/kanbanniang.html" data-v-5775ee02><i class="kbnfont kbn-ban-info info" data-v-5775ee02></i></a> <i class="kbnfont kbn-ban-theme skin" style="display:;" data-v-5775ee02></i></div> <canvas id="banniang" width="240" height="352" class="live2d" style="right:90px;bottom:-20px;opacity:0.9;" data-v-5775ee02></canvas></div> <div class="showBanNiang" style="display:none;" data-v-5775ee02>
    看板娘
  </div></div><canvas id="vuepress-canvas-cursor"></canvas><APlayer audio="" fixed="true" mini="true" autoplay="autoplay" theme="#b7daff" loop="loop" order="random" preload="auto" volume="0.2" mutex="true" lrc-type="1" list-max-height="250" storage-name="vuepress-plugin-meting" id="aplayer-fixed"></APlayer></div></div>
    <script src="/blog/assets/js/app.e4cba77b.js" defer></script><script src="/blog/assets/js/5.1ad0ff24.js" defer></script><script src="/blog/assets/js/1.fe4eac58.js" defer></script><script src="/blog/assets/js/52.c11bc4a4.js" defer></script>
  </body>
</html>
